package com.zyj.hms.pushsample.utils;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class WakeUpUtils {
    public CallBack mCallBack;

    public interface CallBack {
        public void solve(String result);
    }

    public void wakeup(CallBack callBack) {
        this.mCallBack = callBack;
        new Thread(() -> {
            //请求详情
            String mac = "90-09-D0-10-B5-39"; //mac地址
            try {
                int port = 9;
                byte[] macByte = new byte[6];
                String[] ips = mac.split("\\:|\\-");
                for (int i = 0; i < 6; i++) {
                    macByte[i] = (byte) Integer.parseInt(ips[i], 16);
                }
                // 用来存储网络唤醒数据包
                byte[] bys = new byte[6 + 16 * macByte.length];
                for (int i = 0; i < 6; i++) {
                    bys[i] = (byte) 0xff;
                }
                for (int i = 6; i < bys.length; i += macByte.length) {
                    System.arraycopy(macByte, 0, bys, i, macByte.length);
                }
                // 将字符形式的IP地址转换成标准的IP地址
                InetAddress address = InetAddress.getByName("192.168.0.255");
//             InetAddress address = InetAddress.getByName("192.168.1.255");
//            InetAddress address = InetAddress.getByName("255.255.255.255");
                // 生成标准的数据报
                DatagramPacket pack = new DatagramPacket(bys, bys.length, address, port);
                // 创建标准套接字，用来发送数据报
                DatagramSocket socket = new DatagramSocket();
                // 发送魔法包
                socket.send(pack);
                socket.close();
                System.out.println("代码执行完成");
                if (this.mCallBack != null) this.mCallBack.solve("发送指令成功！");
            } catch (Exception e) {
                e.printStackTrace();
                if (this.mCallBack != null) this.mCallBack.solve("发送指令失败！");
            } catch (Throwable e) {
                e.printStackTrace();
                if (this.mCallBack != null) this.mCallBack.solve("发送指令失败！");
            }
        }).start();
    }
}